const express = require("express");
const router = express.Router();
const adminsDao = require("../dao/mysql/AdminsDao");
const dormAdminsDao = require("../dao/mysql/DormAdminsDao");
const studentsDao = require("../dao/mysql/StudentsDao");
const menusDao = require("../dao/mysql/MenusDao");

//html后缀，表示请求的页面
router.get("/logout.do", (req, res) => {
    res.json({
        code: 200,
        msg: "注销成功"
    });
});

router.post("/modifyPassword.do", (req, res, next) => {
    if (req.body.type) {
        let type = req.body.type;
        let obj = {};
        obj.id = req.body.id;
        obj.password = req.body.newPassword
        if (type == "0") {
            adminsDao.update(obj).then(data => {
                res.json({
                    code: 200,
                    msg: "修改成功"
                })
            });
        } else if (type == "1") {
            dormAdminsDao.update(obj).then(data => {
                res.json({
                    code: 200,
                    msg: "修改成功"
                })
            });
        } else if (type == "2") {
            studentsDao.update(obj).then(data => {
                res.json({
                    code: 200,
                    msg: "修改成功"
                })
            });

        }
    }
});
//do后缀，表示处理请求
router.post("/login.do", (req, res, next) => {
    //获取提交的数据
    const { password, account, rem, type } = req.body;
    console.log("login.do");
    //数据验证
    if (!account || account.trim().length < 5) {
        return res.json({
            code: 500,
            msg: "账号长度必须大于5位"
        });
    }
    if (!password || password.length < 3) {
        return res.json({
            code: 500,
            msg: "密码长度必须大于3位"
        });
    }
    if (!type) {
        return res.json({
            code: 500,
            msg: "请选择用户类型"
        });
    }
    let result;//登录查询结果
    let msg;//登录用户信息
    if (type == "0") {
        result = adminsDao.login(account, password);
        msg = "欢迎您，系统管理员";
    } else if (type == "1") {
        result = dormAdminsDao.login(account, password);
        msg = "欢迎您，宿舍管理员";
    } else if (type == "2") {
        result = studentsDao.login(account, password);
        msg = "欢迎您，学生";
    } else {
        return res.json({
            code: 500,
            msg: "用户类型错误"
        });
    }
    if (result) {
        result.then(data => {
            if (!data) {
                return res.json({
                    code: 404,
                    msg: "账号或密码错误"
                });
            }
            if (rem == "1") {
                //req.cookies，获取所有cookie
                //在cookie中保存账号和密码
                //res，响应对象
                res.cookie("account", account, { maxAge: 1000 * 60 * 60 * 24 * 7, signed: true });//maxAge单是毫秒
                res.cookie("password", password, { maxAge: 1000 * 60 * 60 * 24 * 7, signed: true });
            } else {
                // res.cookie("account", "");
                // res.cookie("password", "");
                res.clearCookie("account");
                res.clearCookie("password");
            }
            let app = res.app.locals;
            //登录成功重定向到/admins/list.html
            res.json({
                code: 200,
                msg,
                // 调用 jwt.sign() 生成 JWT 字符串，三个参数分别为：用户信息对象，加密密钥，配置对象
                token: 'Bearer ' + app.jwt.sign({ account: data.account, msg, type: type, role: data.role }, app.secretKey, { expiresIn: '7days' })
            });
        }).catch(err => next(err));
    } else {
        return res.json({
            code: 404,
            msg: "账号或密码错误"
        });
    }
});

router.get("/getInfo.do", (req, res, next) => {
    console.log("/getInfo.do")
    // 查询角色菜单
    // res.app.locals.menus = data;
    // res.app.locals.menu_active = 0;//当前选中的菜单项
    res.json({
        code: 200,
        data: {
            //expressJWT会解析token放到req.user
            loginUser: req.user
        }
    });
})

module.exports = router;